package com.guigui.gulimall.member.service.impl;

import com.guigui.common.constant.BizCodeConstant;
import com.guigui.gulimall.member.dto.LoginDto;
import com.guigui.gulimall.member.dto.RegisterDto;
import com.guigui.gulimall.member.exception.PhoneException;
import com.guigui.gulimall.member.exception.UsernameException;
import com.guigui.gulimall.member.service.MemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;

import com.guigui.gulimall.member.dao.MemberDao;
import com.guigui.gulimall.member.entity.MemberEntity;
import com.guigui.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(RegisterDto registerDto) throws UsernameException,PhoneException{

        MemberEntity member = new MemberEntity();
        String password = registerDto.getPassword();
        String phone = registerDto.getPhone();
        String username = registerDto.getUsername();

        // 校验唯一性
        checkUniquePhone(phone);
        checkUniqueUsername(username);


        Long level = memberLevelService.getDefaultLevel();
        member.setLevelId(level);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        member.setPassword(passwordEncoder.encode(password));
        member.setMobile(phone);
        member.setUsername(username);
        member.setMobile(phone);
        member.setCreateTime(new Date(System.currentTimeMillis()));

        baseMapper.insert(member);
    }

    @Override
    public MemberEntity login(LoginDto loginDto) {

        String account = loginDto.getAccount();
        String password = loginDto.getPassword();
        MemberEntity member = this.getOne(new QueryWrapper<MemberEntity>().eq("username", account).or().eq("mobile", account));
        if (member != null) {
            String enPassword = member.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if (passwordEncoder.matches(password, enPassword)) {
                return member;
            }
        }
        throw new RuntimeException(BizCodeConstant.ErrorCodeStatus.LOGIN_ERR.getMsg());
    }

    public void checkUniqueUsername(String username) {
        MemberEntity entity = this.getOne(new QueryWrapper<MemberEntity>().eq("username", username).last("LIMIT 1"));
        if (entity != null) {
            throw new UsernameException(BizCodeConstant.ErrorCodeStatus.USERNAME_UNIQUE_ERR.getMsg());
        }
    }

    public void checkUniquePhone(String phone) {
        MemberEntity entity = this.getOne(new QueryWrapper<MemberEntity>().eq("mobile", phone).last("LIMIT 1"));
        if (entity != null) {
            throw new PhoneException(BizCodeConstant.ErrorCodeStatus.PHONE_UNIQUE_ERR.getMsg());
        }
    }

}